$(document).ready(function () {
    // 顶部导航下拉菜单
    $(".am-dropdown").hover(function () {
        $(this).children("ul").stop().fadeTo(1, 1);
    }, function () {
        $(this).children("ul").stop().fadeOut(200);
    });
    $(".am-form-select").hover(function () {
        $(".am-selected-content").stop().fadeTo(1, 1);
    }, function () {
        $(".am-selected-content").stop().fadeOut(200);
    });

    // 左侧边栏菜单的选中效果，根据面包屑的内容决定
    var active = $(".am-breadcrumb").find('li');
    var active_li1 = $(".left-side-panel li:contains('" + active.eq(-1).text() + "')");
    var active_li2 = $(".left-side-panel li:contains('" + active.eq(-2).text() + "')");
    if (active_li1.length) active_li1.addClass("ed-active");
    else active_li2.addClass("ed-active");
});

// 返回顶部按钮
backTop = function (btnId) {
    var btn = document.getElementById(btnId);
    var d = document.documentElement;
    var b = document.body;
    window.onscroll = set;
    btn.onclick = function () {
        btn.style.display = "none";
        window.onscroll = null;
        this.timer = setInterval(function () {
            d.scrollTop -= Math.ceil((d.scrollTop + b.scrollTop) * 0.1);
            b.scrollTop -= Math.ceil((d.scrollTop + b.scrollTop) * 0.1);
            if ((d.scrollTop + b.scrollTop) == 0) clearInterval(btn.timer, window.onscroll = set);
        }, 10);
    };
    function set() {
        btn.style.display = (d.scrollTop + b.scrollTop > 100) ? 'block' : "none"
    }
};
backTop('gotop');

var keditor;
function keinit(kid, guest) {
    var items = ['insertorderedlist', 'insertunorderedlist', 'emoticons', 'image'];
    // 如果是游客身份，则富文本编辑器不加载上传图片的按钮
    if (guest) {
        items = ['insertorderedlist', 'insertunorderedlist', 'emoticons'];
    }
    keditor = KindEditor.create(kid, {
        resizeType: 0,
        minHeight: 100,
        height: "100%",
        width: "100%",
        themeType: 'simple',
        filterMode: true,
        htmlTags: {
            font: [],
            span: [],
            div: ['align', '.margin', '.padding', '.text-align', '.text-decoration', '.vertical-align', '.margin-left'],
            table: ['cellspacing', 'cellpadding', 'width', 'height', 'align', '.padding', '.margin', '.text-align',
                '.text-decoration', '.width', '.height', '.border-collapse'],
            'td,th': ['align', 'valign', 'width', 'height', 'colspan', 'rowspan',
                '.text-align', '.text-decoration', '.vertical-align'],
            a: ['href', 'target', 'name'],
            embed: ['src', 'width', 'height', 'type', 'loop', 'quality', '.width', '.height', 'align', 'allowscriptaccess'],
            img: ['src', 'width', 'height', 'alt', 'title', 'align', '.width', '.height'],
            'p,ol,ul,li,blockquote,h1,h2,h3,h4,h5,h6': [
                'align', '.text-align', '.text-decoration', '.vertical-align', '.text-indent', '.margin-left'],
            'pre,hr,br,tbody,tr,strong,b,sub,sup,em,i,u,strike,s,del': []
        },
        allowImageUpload: true,
        allowMediaUpload: false,
        autoHeightMode: false,
        uploadJson: '/media/uploads/comments/attachments',
        items: items,
        afterBlur: function () {
            this.sync();
        }
    });
}

// 封装ajax加载函数
function wlajax(url, type, id, error) {
    $.ajax({
        url: url,
        type: type,
        success: function (t) {
            $(id).html(t);
        },
        error: function () {
            $(id).html(error);
        }
    });
}

// 封装加载模态弹框，在onclick里调用
function wlmodal(url, id, modalid, w, h) {
    $.ajax({
        url: url,
        type: 'get',
        success: function (t) {
            $(id).html(t);
            $(modalid).modal({width: w, height: h});
        }
    });
}
